<template>
	<view>
		<view class="content">
			<view class="share-top"><img class="share-img" :src="poster" mode="widthFix" /></view>
			<view class="share-bot" @click="savePoster()">保存到本地</view>
		</view>
		<view class=""></view>
		<view class=""></view>
		<loading-cover ref="loadingCover"></loading-cover>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				poster: '',
				filePath: ''
			}
		},
		onLoad(option) {
			if (option.poster) {
				this.poster = decodeURIComponent(option.poster);
				this.$nextTick(() => {
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				})
			}
		},
		methods: {
			savePoster() {
				let that = this;
				uni.downloadFile({
					url: this.poster,
					success: res => {
						if (res.statusCode === 200) {
							that.filePath = res.tempFilePath;

							//#ifdef MP
							//获取授权列表，查看是否授权写入相册权限
							uni.getSetting({
								success(res) {
									if (!res.authSetting['scope.writePhotosAlbum']) {
										uni.authorize({
											scope: 'scope.writePhotosAlbum',
											success() {
												that.saveimgtoAlbum()
											}
										})
									} else {
										that.saveimgtoAlbum()
									}
								}
							})
							//#endif

							//#ifndef MP
							uni.saveImageToPhotosAlbum({
								filePath: that.filePath,
								success: function() {
									uni.showToast({
										title: '保存成功',
										icon: 'none'
									});
								}
							});
							//#endif
						}
					}
				})
			},
			saveimgtoAlbum() {
				//图片保存到本地
				let that = this;
				uni.showLoading();
				uni.saveImageToPhotosAlbum({
					filePath: that.filePath,
					success: function(data) {
						uni.hideLoading()
						uni.showToast({
							title: '保存成功',
							icon: 'success',
							duration: 2000
						})
					},
					fail: function(err) {
						uni.hideLoading();
						if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
							uni.openSetting({
								success(settingdata) {
									if (settingdata.authSetting['scope.writePhotosAlbum']) {
										uni.showToast({
											title: '您已授权成功，请重新保存收款码',
											icon: 'success',
											duration: 2000
										})
									} else {
										uni.showToast({
											title: '尚未授权，无法保存海报',
											icon: 'none',
											duration: 2000
										})
									}
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.share-top {
		padding: 30rpx;
	}

	.share-img {
		width: 100%;
	}

	.share-bot {
		margin-top: 34rpx;
		width: 690rpx;
		height: 72rpx;
		line-height: 72rpx;
		background: #FF6A00;
		border-radius: 36rpx;
		margin: 0 auto;
		font-size: 28rpx;
		color: #FFFFFF;
		text-align: center;
	}
</style>